File: /var/www/html/owlcrm/app/Http/Controllers/PropertyController.php
<?php
namespace App\Http\Controllers;
use App\Models\Amenities;
use App\Models\Project;
use App\Models\Property;
use App\Models\User;
use App\Models\PropertyAmenity;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use Validator;
class PropertyController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$main_query = Property::query();
if ($request->ajax()) {
$search_keyword = '';
$status = 'all'; // all status
$request_data = $request->all();
if ($request->has('search_keyword')) {
$search_keyword = $request->search_keyword;
}
// avoid zero column as it's checkbox so we can't sort by it
if ($request->has('order') && $request->order[0]['column'] != 0) {
$sort_column_number = $request->order[0]['column'];
$sort_column_dir = $request->order[0]['dir'];
$sort_column_key = $request->columns[$sort_column_number]['data'];
}
$user = auth()->user();
$main_query = Property::query()
->where(function ($query) {
// Logged-in admin ka apna data
$query->where('user_id', '=', auth()->id())
// Parent ka data
->orWhere('user_id', '=', auth()->user()->parent_id);
// Siblings ka data (same parent_id, excluding logged-in admin)
if (!is_null(auth()->user()->parent_id)) {
$query->orWhereIn('user_id', function ($subQuery) {
$subQuery->select('id')
->from('users')
->where('parent_id', auth()->user()->parent_id) // Same parent_id wale
->where('id', '!=', auth()->id()); // Exclude logged-in admin
});
}
});
$query = $main_query;
if (!empty($search_keyword)) {
$query = $query->where('name', 'LIKE', '%' . $search_keyword . '%');
}
if (!empty($sort_column_key)) {
$query = $query->orderBy($sort_column_key, $sort_column_dir);
} else {
$query = $query->latest();
}
$data = $query->with('get_project')->get();
$count_total = $main_query->count();
$count_filter = $count_total;
return DataTables::of($data)
->addColumn('name', function ($row) {
return $row->name;
})
->addColumn('nature', function ($row) {
return $row->nature;
})
->addColumn('type', function ($row) {
return $row->type;
})
->addColumn('description', function ($row) {
return $row->description;
})
->addColumn('location', function ($row) {
return $row->location;
})
->addColumn('project_id', function ($row) {
return isset($row->get_project) && ($row->get_project->title) ? $row->get_project->title : 'null';
})
->addColumn('action', function ($row) {
if ($row->is_admin !== 1) {
return view('users.properties.partially.delete', ['row' => $row])->render();
}
})
->rawColumns(['action'])
->with([
"recordsTotal" => $count_total,
"recordsFiltered" => $count_filter,
])
->make(true);
}
return view('users.properties.index');
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$currentUser = auth()->user();
// Fetch projects related to the current user, their parent, and siblings
$projects = Project::query()
->where(function ($query) use ($currentUser) {
// Leads created by the current user
$query->where('user_id', $currentUser->id);
// Leads created by the parent user (if parent_id exists)
if ($currentUser->parent_id) {
$query->orWhere('user_id', $currentUser->parent_id);
}
// Leads created by siblings (users with the same parent_id as the current user)
if ($currentUser->parent_id) {
// Fetch siblings that share the same parent_id, excluding leads created by other admins with different parent_id
$query->orWhereIn('user_id', User::where('parent_id', $currentUser->parent_id)
->where('id', '!=', $currentUser->id) // Exclude the current user
->pluck('id'));
}
})
->get();
// Fetch amenities related to the current user, their parent, and siblings
$amenities = Amenities::query()
->where(function ($query) use ($currentUser) {
// Leads created by the current user
$query->where('user_id', $currentUser->id);
// Leads created by the parent user (if parent_id exists)
if ($currentUser->parent_id) {
$query->orWhere('user_id', $currentUser->parent_id);
}
// Leads created by siblings (users with the same parent_id as the current user)
if ($currentUser->parent_id) {
// Fetch siblings that share the same parent_id, excluding leads created by other admins with different parent_id
$query->orWhereIn('user_id', User::where('parent_id', $currentUser->parent_id)
->where('id', '!=', $currentUser->id) // Exclude the current user
->pluck('id'));
}
})
->get();
// Fetch property amenities (assuming no user relation needed here)
$property_amenities = PropertyAmenity::all();
return view('users.properties.create', compact('projects', 'amenities', 'property_amenities'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'project_id' => 'required|exists:projects,id',
'nature' => 'required',
'type' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$property = new Property();
$currentUserId = getAdminId();
$property ->created_by = auth()->id();
$property ->user_id = isset(auth()->user()->parent_id) ? auth()->user()->parent_id : auth()->id() ;
if (isset($property)) {
$property->name = $request->name;
$property->nature = $request->nature;
$property->type = $request->type;
$property->description = $request->description;
$property->location = $request->location;
$property->project_id = $request->project_id;
$property->save();
$property_id = $property->id;
$property_amenities = new PropertyAmenity();
$property_amenities->property_id = $property_id;
$property_amenities->amenity_id = $request->amenity_id;
$property_amenities->save();
return redirect()->route('properties.index')->with('success', 'Property Add Successfully');
} else {
return back()->with('error', 'Property does not exist');
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$property = Property::findorFail($id);
$property_amenities = PropertyAmenity::where('property_id', $id)->first();
return view('users.properties.show', compact('property', 'property_amenities'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$property = Property::findorFail($id);
$projects = Project::all();
$amenities = Amenities::all();
// $property_amenities = PropertyAmenity::where('property_id', $id)->first();
$property_amenities = PropertyAmenity::where('property_id', $id)->pluck('amenity_id')->toArray();
return view('users.properties.edit', compact('property', 'projects', 'amenities', 'property_amenities'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'project_id' => 'required|exists:projects,id',
'nature' => 'required',
'type' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$property = Property::find($id);
if (isset($property)) {
$property->name = $request->name;
$property->nature = $request->nature;
$property->type = $request->type;
$property->description = $request->description;
$property->location = $request->location;
$property->project_id = $request->project_id;
$property->save();
$property_amenities = PropertyAmenity::where('property_id', $property->id)->first();
if ($property_amenities) {
$property_amenities->amenity_id = $request->amenity_id;
$property_amenities->save();
} else {
$property_amenities = new PropertyAmenity();
$property_amenities->property_id = $property->id;
$property_amenities->amenity_id = $request->amenity_id;
$property_amenities->save();
}
return redirect()->route('properties.index')->with('success', 'Property has been updated successfully');
} else {
return back()->with('error', 'Property does not exist');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$property = Property::find($id);
if (isset($property)) {
$property->delete();
PropertyAmenity::where('property_id', $id)->delete();
return redirect()->back()->with('success', 'Property has been deleted successfully.');
} else {
return back()->with('error', 'Property does not exist');
}
}
public function assignAmenityToProperty(Request $request, $property_id)
{
// dd($request->all(), $property_id);
$validator = Validator::make($request->all(), [
'amenity_id' => 'required|exists:amenities,id',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$property_amenities = PropertyAmenity::where('property_id', $property_id)->first();
if ($property_amenities) {
$property_amenities->amenity_id = $request->amenity_id;
$property_amenities->save();
} else {
$property_amenities = new PropertyAmenity();
$property_amenities->property_id = $property_id;
$property_amenities->amenity_id = $request->amenity_id;
$property_amenities->save();
return redirect()->route('properties.index')->with('success', 'Property has been updated successfully');
}
}
}